package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.comac.mom.bean.EquipFaultCause;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipFaultlCauseDao;
import cc.comac.mom.entity.EquipFaultCauseEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipFaultCauseDataHandler extends ServiceImpl<EquipFaultlCauseDao, EquipFaultCauseEntity> {

	@Autowired
	private EquipFaultlCauseDao equipFaultlCauseDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;

	public ResultBean insertFaultCause(EquipFaultCause equipFaultlCause) {
		ResultBean resultBean = new ResultBean(true);
		if (StringUtils.isEmpty(equipFaultlCause.getFaultCauseCode())) {
			throw new RuntimeException("故障原因代码不能为空！");
		}
		QueryWrapper<EquipFaultCauseEntity> queryWrapper1 = new QueryWrapper<EquipFaultCauseEntity>();
		queryWrapper1.eq("row_status", 1);
		queryWrapper1.eq("fault_cause_describe", equipFaultlCause.getFaultCauseDescribe());
		List<EquipFaultCauseEntity> selectList1 = equipFaultlCauseDao.selectList(queryWrapper1);
		if (selectList1.size() > 0) {
			throw new RuntimeException("故障原因描述" + equipFaultlCause.getFaultCauseDescribe() + "已存在！");
		}
		QueryWrapper<EquipFaultCauseEntity> queryWrapper = new QueryWrapper<EquipFaultCauseEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("fault_cause_code", equipFaultlCause.getFaultCauseCode());
		List<EquipFaultCauseEntity> selectList = equipFaultlCauseDao.selectList(queryWrapper);
		if (selectList.size() > 0) {
			throw new RuntimeException("故障原因代码" + equipFaultlCause.getFaultCauseCode() + "已存在！");
		}
		baseEntityDataUtil.setBaseEntityData(equipFaultlCause);
		int insert = equipFaultlCauseDao.insert(equipFaultlCause);
		if (insert != 1) {
			resultBean.setErrorMessge("新增失败，新增数量" + insert);
		}
		return resultBean;
	}

	public int updateFaultCause(EquipFaultCause equipFaultlCause) {
		if (equipFaultlCause.getId() == 0) {
			throw new RuntimeException("当前未选择数据！,id为0");
		}

		if (!StringUtils.isEmpty(equipFaultlCause.getFaultCauseDescribe())) {
			QueryWrapper<EquipFaultCauseEntity> queryWrapper = new QueryWrapper<EquipFaultCauseEntity>();
			queryWrapper.eq("row_status", 1);
			queryWrapper.eq("fault_cause_describe", equipFaultlCause.getFaultCauseDescribe());
			queryWrapper.ne("fault_cause_id", equipFaultlCause.getId());
			List<EquipFaultCauseEntity> selectList = equipFaultlCauseDao.selectList(queryWrapper);
			if (selectList.size() > 0) {
				throw new RuntimeException("当前故障原因描述" + equipFaultlCause.getFaultCauseCode() + "已存在！");
			}
		}

		if (!StringUtils.isEmpty(equipFaultlCause.getFaultCauseCode())) {
			QueryWrapper<EquipFaultCauseEntity> queryWrapper = new QueryWrapper<EquipFaultCauseEntity>();
			queryWrapper.eq("row_status", 1);
			queryWrapper.eq("fault_cause_code", equipFaultlCause.getFaultCauseCode());
			queryWrapper.ne("fault_cause_id", equipFaultlCause.getId());
			List<EquipFaultCauseEntity> selectList = equipFaultlCauseDao.selectList(queryWrapper);
			if (selectList.size() > 0) {
				throw new RuntimeException("当前故障原因代码" + equipFaultlCause.getFaultCauseCode() + "已存在！");
			}
		}
		baseEntityDataUtil.updateBaseEntityData(equipFaultlCause);
		int quatity = equipFaultlCauseDao.updateById(equipFaultlCause);
		if (quatity != 1) {
			throw new RuntimeException("更新失败，更新数量为" + quatity);
		}
		return quatity;
	}

	public boolean batchDelete(List<EquipFaultCause> list) {

		if (list.size() == 0) {
			throw new RuntimeException("请选择数据！");
		}

		// 预留删除校验
		// .............

		List<EquipFaultCauseEntity> list1 = new ArrayList<EquipFaultCauseEntity>();
		for (EquipFaultCause equipFaultlCause : list) {
			EquipFaultCauseEntity equipFaultlCauseEntity = new EquipFaultCauseEntity();
			equipFaultlCauseEntity.setId(equipFaultlCause.getId());
			equipFaultlCauseEntity.setRowStatus(0);
			baseEntityDataUtil.updateBaseEntityData(equipFaultlCauseEntity);
			list1.add(equipFaultlCauseEntity);
		}
		boolean updateBatchById = updateBatchById(list1, list1.size());
		if (!updateBatchById) {
			throw new RuntimeException("删除失败");
		}
		return updateBatchById;
	}

	public ResultDev selectSomeField (EquipFaultCause equipFaultlCause) {
		
		if (equipFaultlCause.getPageNum() == null || equipFaultlCause.getPageNum() == 0) {
			equipFaultlCause.setPageNum(1);
		}
		if (equipFaultlCause.getPageSize() == null || equipFaultlCause.getPageSize() == 0) {
			equipFaultlCause.setPageSize(10);
		}
		ResultDev resultDev = new ResultDev();
		QueryWrapper<EquipFaultCauseEntity> queryWrapper = new QueryWrapper<EquipFaultCauseEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.orderByDesc("creation_time");
		if (StringUtils.isNotEmpty(equipFaultlCause.getFaultCauseCode())) {
			queryWrapper.like("fault_cause_code", equipFaultlCause.getFaultCauseCode());
		}
		if (StringUtils.isNotEmpty(equipFaultlCause.getFaultCauseDescribe())) {
			queryWrapper.like("fault_cause_describe",equipFaultlCause.getFaultCauseDescribe());
		}
		Page<EquipFaultCauseEntity> page = new Page<EquipFaultCauseEntity>(equipFaultlCause.getPageNum(),equipFaultlCause.getPageSize(),true);
		IPage<EquipFaultCauseEntity> selectPage = equipFaultlCauseDao.selectPage(page, queryWrapper);
		resultDev.setEntityList(selectPage.getRecords());
		resultDev.setPageCount((int)selectPage.getPages());
		resultDev.setTotalQuantity((int)selectPage.getTotal());
		return resultDev;
		
	}
	
	public ResultDev selectAll() {
		ResultDev resultDev = new ResultDev();
		QueryWrapper<EquipFaultCauseEntity> queryWrapper = new QueryWrapper<EquipFaultCauseEntity>();
		queryWrapper.eq("row_status", 1);
		List<EquipFaultCauseEntity> selectList = equipFaultlCauseDao.selectList(queryWrapper);
		resultDev.setEntityList(selectList);
		return resultDev;
	}
}
